<script setup>
import { ref, watch, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

// 定义图标组件映射
const IconMap = ElementPlusIconsVue

// 侧边栏状态管理
const isCollapse = defineModel('collapse', { type: Boolean, default: false })

// 当前选中的分类
const selectedCategory = ref('main')

// 监听路由变化，更新当前菜单项
const route = useRoute()
const router = useRouter()

// 获取当前活动路由
const currentRoute = computed(() => {
  return route.path
})

// 从路由中提取菜单数据和分类信息
const menuItems = computed(() => {
  // 获取所有路由记录
  const routes = router.options.routes
  const items = []
  const categories = new Set()
  
  // 递归遍历路由树
  const traverseRoutes = (routes, prefix = '') => {
    routes.forEach(route => {
      // 检查是否有元数据且包含分类信息
      if (route.meta && route.meta.category) {
        items.push({
          index: route.name || route.path,
          label: route.meta.title || route.name,
          icon: route.meta.icon || 'Document',
          path: prefix + route.path,
          category: route.meta.category
        })
        categories.add(route.meta.category)
      }
      
      // 递归处理子路由
      if (route.children && route.children.length > 0) {
        traverseRoutes(route.children, prefix + route.path)
      }
    })
  }
  
  traverseRoutes(routes)
  
  // 将分类信息添加到结果中
  return {
    items,
    categories: Array.from(categories)
  }
})

// 递归查找菜单项
const findMenuItemByPath = (items, path) => {
  for (const item of items) {
    if (item.path === path) {
      return item
    }
  }
  return null
}

// 获取当前菜单项的选中状态
const isMenuItemActive = (item) => {
  return route.path === item.path
}

// 初始化分类
const initCategory = () => {
  const menuItem = findMenuItemByPath(menuItems.value.items, route.path)
  if (menuItem && menuItem.category) {
    selectedCategory.value = menuItem.category
  }
}

// 初始化分类
initCategory()

// 监听路由变化
watch(route, (newRoute) => {
  // 根据当前路由更新选中的分类
  const menuItem = findMenuItemByPath(menuItems.value.items, newRoute.path)
  if (menuItem && menuItem.category) {
    selectedCategory.value = menuItem.category
  }
})

// 处理分类点击
const handleCategoryClick = (category) => {
  selectedCategory.value = category
}

// 处理菜单点击
const handleMenuClick = (index) => {
  const menuItem = menuItems.value.items.find(item => item.path === index)
  if (menuItem && menuItem.path) {
    router.push(menuItem.path)
  }
}

// 获取所有分类
const categories = computed(() => {
  return menuItems.value.categories
})

// 获取当前分类下的菜单项
const currentCategoryMenuItems = computed(() => {
  return menuItems.value.items.filter(item => item.category === selectedCategory.value)
})

// 从路由中提取分类信息和图标
const categoryInfo = computed(() => {
  const info = {}
  const routes = router.options.routes
  
  // 递归遍历路由树提取分类信息
  const traverseRoutes = (routes) => {
    routes.forEach(route => {
      if (route.meta && route.meta.category) {
        // 如果还没有该分类的信息，则添加
        if (!info[route.meta.category]) {
          info[route.meta.category] = {
            label: route.meta.categoryLabel || route.meta.category,
            icon: route.meta.icon || 'Document'
          }
        }
      }
      
      // 递归处理子路由
      if (route.children && route.children.length > 0) {
        traverseRoutes(route.children)
      }
    })
  }
  
  traverseRoutes(routes)
  return info
})
</script>

<template>
  <aside class="sidebar-container" :class="{ 'sidebar-collapse': isCollapse }">
    <!-- 侧边栏顶部 -->
    <div class="sidebar-header">
      <div class="logo" v-if="!isCollapse">
        <span class="logo-text">智教育平台</span>
      </div>
    </div>
    
    <div class="sidebar-content">
      <!-- 左侧分类 -->
      <div class="sidebar-categories">
        <el-menu
          class="category-menu"
          default-active="main"
          @select="handleCategoryClick"
          text-color="#fff"
          background-color="#1d2a4a"
          active-text-color="#4096ff"
        >
          <el-menu-item
            v-for="category in categories"
            :key="category"
            :index="category"
            class="category-item"
          >
            <div class="category-card">
              <component :is="IconMap[categoryInfo[category]?.icon] || IconMap['Document']" class="category-icon"></component>
              <span class="category-label">{{ categoryInfo[category]?.label || category }}</span>
            </div>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 右侧导航项 -->
      <div class="sidebar-items">
        <el-menu
          class="items-menu"
          :default-active="currentRoute"
          :collapse="false"
          @select="handleMenuClick"
          text-color="#fff"
          background-color="#273352"
          active-text-color="#4096ff"
          unique-opened
        >
          <template v-for="item in currentCategoryMenuItems" :key="item.index">
            <el-menu-item
              :index="item.path"
            >
              <component :is="IconMap[item.icon] || IconMap['Document']" class="menu-item-icon"></component>
              <span>{{ item.label }}</span>
            </el-menu-item>
          </template>
        </el-menu>
      </div>
    </div>
  </aside>
</template>

<style scoped>
/* 侧边栏样式 */
.sidebar-container {
  display: flex;
  flex-direction: column;
  width: 220px;
  background-color: #273352;
  color: #fff;
  transition: all 0.3s;
  height: 100%;
  overflow: hidden;
  position: relative;
}

.sidebar-header {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0 10px;
  height: 60px;
  background-color: #1d2a4a;
  border-bottom: 1px solid #334166;
  width: 100%;
}

.logo {
  margin: 0;
}

.logo-text {
  font-size: 18px;
  font-weight: bold;
  color: #1890ff;
}

.sidebar-content {
  display: flex;
  flex: 1;
  overflow: hidden;
  height: calc(100% - 60px);
}

.el-menu-item.is-active .category-card {
  background-color: rgba(64, 150, 255, 0.1);
  border-radius: 4px;
}

.el-menu-item.is-active .category-icon,
.el-menu-item.is-active .category-label {
  color: #ffffff;
}

.el-menu-item:hover .category-card {
  background-color: #334166;
}

.el-menu-item:hover .category-icon,
.el-menu-item:hover .category-label {
  color: #ffffff;
}

.sidebar-collapse {
  width: 64px;
}

.sidebar-collapse .sidebar-content {
  flex-direction: column;
}

.sidebar-collapse .sidebar-categories {
  width: 100%;
}

.sidebar-collapse .sidebar-items {
  display: none;
}

.sidebar-collapse .sidebar-header {
  justify-content: center;
}

.sidebar-collapse .logo {
  display: none;
}

.sidebar-categories {
  width: 60px;
  background-color: #1d2a4a;
  border-right: 1px solid #334166;
  overflow-y: hidden;
  padding-top: 5px;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.category-menu {
  height: 100%;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: #334166 transparent;
}

.category-item {
  display: flex !important;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10px 4px !important;
  height: auto !important;
  gap: 4px;
}

.category-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: transparent;
  border-radius: 4px;
  padding: 8px 4px;
  transition: all 0.3s;
  width: 48px;
  box-sizing: border-box;
  position: relative;
}

.category-card::before {
  content: '';
  position: absolute;
  left: -6px;
  top: 8px;
  height: calc(100% - 16px);
  width: 3px;
  background-color: transparent;
  border-radius: 0 2px 2px 0;
  transition: all 0.3s;
  opacity: 0;
}

.el-menu-item.is-active .category-card::before {
  background-color: #4096ff;
  opacity: 1;
}

.category-icon {
  font-size: 20px !important;
  width: 20px !important;
  height: 20px !important;
  margin-bottom: 4px;
  color: #bfc9e0;
}

.category-label {
  font-size: 10px;
  line-height: 1.1;
  text-align: center;
  color: #bfc9e0;
  transform: scale(0.9);
  white-space: nowrap;
}

/* 调整左侧分类图标大小 */
.category-icon {
  font-size: 18px !important;
  width: 18px !important;
  height: 18px !important;
}

/* 强制覆盖Element Plus的默认图标大小 */
:deep(.el-icon) {
  font-size: 12px !important;
  width: 12px !important;
  height: 12px !important;
}

:deep(.el-menu-item .el-icon),
:deep(.el-sub-menu__title .el-icon) {
  font-size: 12px !important;
  width: 12px !important;
  height: 12px !important;
}

/* 修复分类菜单样式 */
:deep(.category-menu) {
  border-right: none !important;
  padding: 5px 0;
}

:deep(.category-menu .el-menu-item) {
  padding: 0 !important;
  margin: 5px 6px !important;
  height: auto !important;
  min-height: 60px !important;
  border-radius: 4px !important;
}

:deep(.category-menu .el-menu-item.is-active) {
  background-color: transparent !important;
}

:deep(.category-menu .el-menu-item:hover) {
  background-color: transparent !important;
}

:deep(.items-menu .el-icon svg),
:deep(.items-menu .el-sub-menu__title svg),
:deep(.items-menu .el-menu-item svg),
:deep(.items-menu svg),
:deep(.el-sub-menu .el-menu-item svg) {
  font-size: 12px !important;
  width: 12px !important;
  height: 12px !important;
  max-width: 12px !important;
  max-height: 12px !important;
  min-width: 12px !important;
  min-height: 12px !important;
}

.sidebar-items {
  width: 160px;
  overflow-y: auto;
  height: 100%;
}

.items-menu {
  height: 100%;
  border-right: none;
  background-color: transparent;
}

.items-menu .el-menu-item {
  height: 40px;
  line-height: 40px;
  padding-left: 16px !important;
  padding-right: 8px !important;
  color: #bfc9e0;
  font-size: 13px;
}

.items-menu .el-menu-item:hover {
  background-color: #334166;
  color: #fff;
}

.items-menu .el-menu-item.is-active {
  background-color: #334166;
  color: #fff;
  border-right: 2px solid #409eff;
}

/* 调整右侧导航条图标大小 */
.menu-item-icon {
  font-size: 12px !important;
  width: 12px !important;
  height: 12px !important;
  margin-right: 6px !important;
}

/* 统一二级菜单样式 */
/* 统一所有图标大小 */
:deep(.items-menu .el-icon),
:deep(.items-menu .el-icon svg),
:deep(.sub-menu-icon) {
  font-size: 12px !important;
  width: 12px !important;
  height: 12px !important;
  margin-right: 6px !important;
  display: inline-block !important;
}

:deep(.items-menu .el-sub-menu__title) {
  height: 40px !important;
  line-height: 40px !important;
  padding-left: 16px !important;
  padding-right: 8px !important;
  color: #bfc9e0 !important;
  font-size: 13px !important;
  background-color: transparent !important;
}

:deep(.items-menu .el-sub-menu__title:hover) {
  background-color: #334166 !important;
  color: #fff !important;
}

:deep(.items-menu .el-menu-item) {
  height: 36px !important;
  line-height: 36px !important;
  padding-left: 16px !important;
  padding-right: 8px !important;
  font-size: 12px !important;
  color: #bfc9e0 !important;
}

:deep(.items-menu .el-menu-item:hover) {
  background-color: #334166 !important;
  color: #fff !important;
}

:deep(.items-menu .el-menu-item.is-active) {
  background-color: #334166 !important;
  color: #fff !important;
  border-right: 2px solid #409eff !important;
}

:deep(.items-menu .el-sub-menu .el-menu-item) {
  height: 32px !important;
  line-height: 32px !important;
  padding-left: 28px !important;
  font-size: 12px !important;
  color: #bfc9e0 !important;
}

:deep(.items-menu .el-sub-menu__icon-arrow) {
  font-size: 12px !important;
  right: 8px !important;
}

/* 自定义滚动条样式 */
.category-menu::-webkit-scrollbar,
.sidebar-items::-webkit-scrollbar {
  width: 4px;
}

.category-menu::-webkit-scrollbar-thumb,
.sidebar-items::-webkit-scrollbar-thumb {
  background-color: #334166;
  border-radius: 2px;
}

.category-menu::-webkit-scrollbar-track,
.sidebar-items::-webkit-scrollbar-track {
  background-color: transparent;
}
</style>